package com.zjj.learn.javaplus.pkg07.demo701;

import java.io.IOException;
import java.net.*;
import java.nio.charset.StandardCharsets;

/**
 * UDP 发送方
 **/
public class UDPSender {

    public static void main(String[] args) throws IOException {
        String message = "周末约吗？";
        //将字符串转化成字节数组
        byte[] buf = message.getBytes(StandardCharsets.UTF_8);
        //创建数据包对象
        DatagramPacket packet = new DatagramPacket(buf, buf.length, InetAddress.getLocalHost(), 6666);
        //创建发送端发送对象
        DatagramSocket socket = new DatagramSocket();
        //发送数据
        socket.send(packet);
        //关闭发送对象释放端口
        socket.close();
    }
}
